package com.qiangesoft.ocr.utils;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationResponse;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;

/**
 * 腾讯云ocr识别
 *
 * @author qiangesoft
 * @date 2025-03-27
 */
public class TencentCloudOcr {

    private static final String SECRET_ID = "xxx";

    private static final String SECRET_KEY = "xxx ";

    private static final String REGION_NAME = "ap-shanghai";

    /**
     * 调用OCR识别身份证信息
     * https://cloud.tencent.com/document/product/866/33524
     *
     * @param imgUrl
     * @return
     * @throws TencentCloudSDKException
     */
    public static IDCardOCRResponse idCardOCR(String imgUrl) throws TencentCloudSDKException {
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("ocr.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        OcrClient client = new OcrClient(cred, REGION_NAME, clientProfile);

        IDCardOCRRequest req = new IDCardOCRRequest();
        req.setImageUrl(imgUrl);
        req.setCardSide("FRONT");
//        req.setCardSide("BACK");
        return client.IDCardOCR(req);
    }

    /**
     * 调用身份信息核验API
     * https://cloud.tencent.com/document/product/1007/33188
     *
     * @param name
     * @param idCard
     * @return
     * @throws TencentCloudSDKException
     */
    public static IdCardVerificationResponse idCardVerification(String name, String idCard) throws TencentCloudSDKException {
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("faceid.tencentcloudapi.com");
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        FaceidClient client = new FaceidClient(cred, REGION_NAME, clientProfile);

        IdCardVerificationRequest req = new IdCardVerificationRequest();
        req.setName(name);
        req.setIdCard(idCard);
        return client.IdCardVerification(req);
    }

    public static void main(String[] args) throws TencentCloudSDKException {
        IDCardOCRResponse idCardOCRResponse = idCardOCR("https://www.fsf.com");
        IdCardVerificationResponse idCardVerificationResponse = idCardVerification("xxx", "22222");
    }

}